package com.ziyun.pop.modules.storeMaterials.dao;

import com.ziyun.pop.modules.purchase.entity.StorePurchaseApplyDetailEntity;
import com.ziyun.pop.modules.purchase.entity.StorePurchaseApplyEntity;
import com.ziyun.pop.modules.purchase.entity.dto.storePurchaseAutoDto;
import com.ziyun.pop.modules.storeMaterials.entity.*;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ziyun.pop.modules.storeMaterials.entity.vo.StoreMaterialInDetailVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 门店耗材入库表
 * 
 * @author guizhao
 * @email 296588519@qq.com
 * @date 2018-10-04 10:03:15
 */
public interface StoreMaterialInDao extends BaseMapper<StoreMaterialInEntity> {

    List<StoreMaterialInDetailVo> selectPurchaseApplyDetail(@Param("inNo") String inNo);


    StoreMaterialInDetailEntity getOutMaterialDetail(@Param("inNo")String inNo, @Param("materialId")String materialId,@Param("inId")Integer inId);


     @Select(" select * from store_stock_material where stock_id = #{stockId} and material_id = #{materialId} ")
     StoreStockMaterialEntity getMaterialByStockId(@Param("stockId") Integer stockId, @Param("materialId") String materialId);


     @Update(" update store_stock_material set material_quantity = material_quantity + ${quantity},money = money + ${totalMoney} where material_id = #{materialId} and stock_id = #{stockId} ")
    Integer updateStoreStockMaterial(@Param("stockId") Integer stockId,@Param("materialId") String materialId,@Param("quantity") BigDecimal quantity,@Param("totalMoney") BigDecimal totalMoney);



    BigDecimal getStockQuantity(@Param("materialId")String materialId,@Param("stockId")Integer stockId);

    BigDecimal getCostByMaterialId(@Param("materialId")String materialId,@Param("stockId")Integer stockId);

    @Update(" update store_stock_material set cost = #{Cost} where material_id = #{materialId} and stock_id = #{stockId} ")
    Integer updateCostToStoreStockMaterial(@Param("Cost")BigDecimal Cost,@Param("materialId")String materialId,@Param("stockId")Integer stockId);


    @Select(" select DISTINCT unit_money from store_purchase_apply_detail where material_id= #{materialId} and apply_no = #{inNo} ")
    BigDecimal getCostByMaterialIdAndApplyNo(@Param("inNo")String inNo,@Param("materialId")String materialId);


    int getOutIdByApplyId(@Param("outNo")String outNo);


    String getInNoByinId(@Param("inId")Integer inId);



    List<StoreMaterialOutDetailEntity> sleectStoreOutDetail(@Param("inNo") String inNo);



    List<storePurchaseAutoDto> sleectStoreOutDetailByApply(@Param("inNo") String inNo);

    List<StoreMaterialInEntity> getStatus(@Param("inNo") String inNo);

    /**
     * 查询仓库
     * @param sellerId
     * @param type 仓库类型，生产仓，售后仓
     * @return
     */
    ErpStockEntity getStockBySellerId(@Param("sellerId")Long sellerId, @Param("type")int type);

    /**
     * 根据采购申请单号、耗材ID，查询采购耗材
     * @param applyNo
     * @param materialId
     * @return
     */
    StorePurchaseApplyDetailEntity getPurchaseApplyDetail(@Param("applyNo")String applyNo, @Param("materialId")String materialId);

    void updateByMap(@Param("map") Map<String,Object> map, @Param("inId")Integer inId);

    void updatePurchaseApplyDetail(@Param("map")Map<String,Object> map, @Param("applydetailId")Integer applydetailId);

    /**
     * 获取 采购申请单
     * @param inNo
     * @return
     */
    StorePurchaseApplyEntity getPurchaseApplyByNo(String inNo);

    /**
     * 更新采购申请单
     * @param map
     * @param applyId
     */
    void updatePurchaseApply(@Param("map")Map<String,Object> map, @Param("applyId")Integer applyId);

    List<StoreMaterialInEntity> getStoreMaterialInList(@Param("inNo")String inNo);

    @Select(" select * from store_material_in where in_no = #{inNo} and type = #{Enumtype} and status = 2 ")
    List<StoreMaterialInEntity> getStoreMaterialInListByStatus(@Param("inNo")String inNo,@Param("Enumtype")Integer Enumtype);


    @Select(" select * from store_material_in where in_no like '${inNo}' and type = #{Enumtype} and status = 2 ")
    List<StoreMaterialInEntity> getStoreMaterialListByEnumTypes(@Param("inNo")String inNo,@Param("Enumtype")Integer Enumtype);



    List<StoreMaterialInEntity> getStoreMaterialInListByStatusNoState(@Param("inNo")String inNo,@Param("EnumTypeS")Integer EnumTypeS);


    @Select(" select distinct total_money from store_material_in where in_no = #{inNo}")
    BigDecimal getTotalMoney(@Param("inNo") String inNo);


    Map<String,String> getStorePurchaseApplyByInNo(@Param("applyNo") String inNo);

    Map<String,String> getOutMarks(@Param("inNo") String inNo);

    @Select(" select receive_quantity from store_purchase_apply_detail where apply_no = #{inNo} and material_id = #{materialId} ")
    BigDecimal getpurchaseapplyDetailReciveNum(@Param("materialId") String materialId,@Param("inNo") String inNo);


    @Update(" update store_purchase_apply_detail set receive_quantity = ${receQuantity} where material_id = #{materialId} and apply_no = #{inNo} ")
    Integer updateReceiveQuantity(@Param("receQuantity") BigDecimal receQuantity,@Param("materialId") String materialId,@Param("inNo") String inNo);

    @Update(" update store_purchase_apply set status = #{status} where apply_no = #{apply_NO}")
    Integer updateStorePurchaseApplyStatus(@Param("apply_NO") String apply_NO,@Param("status") Integer status);

    @Select(" select * from store_material_in where in_no = #{inNo} and status = 2 ")
    List<StoreMaterialInEntity> getAlreadyInAndState(@Param("inNo") String inNo);

    @Select(" select stock_id from store_material_in where in_id = ${inId} ")
    Integer getStockByInId(@Param("inId") Integer inId);

    @Select(" select * from store_material_in where in_id = ${inId} ")
    StoreMaterialInEntity getInfoData(@Param("inId") Integer inId);


    @Update("update store_material_in set purchase_images = #{ids} where in_id = #{inId}")
    Integer saveOtherAccessory(@Param("inId") Integer inId, @Param("ids") String ids);
}
